﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.DataAccess
{
    public class SubjectDAO
    {
        TestOnlineDBEntities db = new TestOnlineDBEntities();

        public List<Subject> Select()
        {
            try
            {
                var subjects = db.Subjects.ToList();
                return subjects;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Subject SelectBySubjectId(int subjectId)
        {
            try
            {
                var subject = db.Subjects.FirstOrDefault(u => u.SubjectId == subjectId);
                return subject;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Subject SelectByCode(string code)
        {
            try
            {
                var subject = db.Subjects.Where(s => s.Code.ToLower().Equals(code.ToLower())).FirstOrDefault();
                return subject;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Subject SelectByName(string name)
        {
            try
            {
                var subject = db.Subjects.FirstOrDefault(s => s.Name.ToLower().Equals(name.ToLower()));
                return subject;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Update(Subject subject)
        {
            try
            {
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Insert(Subject subject)
        {
            try
            {
                db.Subjects.AddObject(subject);
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public int Delete(int subjectId)
        {
            try
            {
                var subject = db.Subjects.Where(s => s.SubjectId == subjectId).FirstOrDefault();

                foreach (var section in subject.Sections)
                {
                    // Delete SectionQuestion.
                    section.SectionQuestions.ToList().ForEach(db.DeleteObject);
                }

                // Delete Sections.
                subject.Sections.ToList().ForEach(db.DeleteObject);

                foreach (var question in subject.Questions)
                {
                    // Delete TestQuestions.
                    question.TestQuestions.ToList().ForEach(db.DeleteObject);

                    // Delete UserAnswers.
                    question.UserAnswers.ToList().ForEach(db.DeleteObject);

                    // Delete Answers.
                    question.Answers.ToList().ForEach(db.DeleteObject);

                    // Delete Matches.
                    question.Matches.ToList().ForEach(db.DeleteObject);

                    // Delete Subject's Reports.
                    question.Reports.ToList().ForEach(db.DeleteObject);
                }

                // Delete Questions.
                subject.Questions.ToList().ForEach(db.DeleteObject);

                foreach (var discussion in subject.Discussions)
                {
                    // Delete Discussion's Violations.
                    discussion.Reports.ToList().ForEach(db.DeleteObject);
                }

                foreach (var test in subject.Tests)
                {
                    // Delete test's Violations.
                    test.Reports.ToList().ForEach(db.DeleteObject);
                }

                foreach (var section in subject.Sections)
                {
                    // Delete section's Violations.
                    section.Reports.ToList().ForEach(db.DeleteObject);
                }

                // Delete Discussions.
                subject.Discussions.ToList().ForEach(db.DeleteObject);

                foreach (var test in subject.Tests)
                {
                    // Delete UserTests.
                    test.UserTests.ToList().ForEach(db.DeleteObject);
                }

                // Delete Tests.
                subject.Tests.ToList().ForEach(db.DeleteObject);

                // Delete Participants.
                subject.Participants.ToList().ForEach(db.DeleteObject);

                // Delete subject.
                db.Subjects.DeleteObject(subject);

                var result = db.SaveChanges();

                return result > 0 ? 1 : 0;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}